const Service = require('egg').Service

class MoocCourseService extends Service {
  // 获取课程详情
  async getCourseDetail(courseId) {
    const { ctx, app } = this
    const res = await ctx.find('Course', {
      attributes: { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] },
      include: [
        {
          model: app.model.Teacher,
          attributes: { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] }
        },
        {
          model: app.model.CourseColumn,
          attributes: ['channelName']
        },
        {
          model: app.model.CourseCategory,
          attributes: ['categoryName']
        }
      ],
      where: {
        courseId
      }
    })
    return res ? res.dataValues : res
  }

  // 获取课程列表
  async getCourseList(query) {
    const { ctx, app } = this
    const { Op } = app.Sequelize
    const { name, teacherName, startTime, endTime } = query
    let where = {}
    if (startTime && endTime) {
      where.createTime = {
        [Op.between]: [startTime, endTime]
      }
    }
    // 添加模糊字段
    const fuzzyField = { teacherName, name }
    where = ctx.helper.addFuzzyField(fuzzyField, where)
    const attributes = { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] }
    const include = [
      {
        model: app.model.CourseColumn,
        attributes: ['channelName']
      },
      {
        model: app.model.CourseCategory,
        attributes: ['categoryName']
      }
    ]
    const options = { where, attributes, include }
    const courseRes = await ctx.pageFind('Course', query, options)
    return courseRes
  }

  // 获取用户评价列表
  async getCommentList(query) {
    const { ctx } = this
    const { courseId } = query
    const where = {
      courseId
    }
    const attributes = { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] }
    const options = { where, attributes }
    const res = await ctx.pageFind('CourseComment', query, options)
    return res
  }

  // 获取课程分析列表
  async getTerminfoList(courseId) {
    const { ctx } = this
    const terminfoRes = await ctx.find('CourseTerminfo', {
      attributes: { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] },
      where: {
        courseId
      },
      order: [['startTime', 'DESC']]
    }, 'findAll')
    if (terminfoRes) {
      return terminfoRes.map(item => item.dataValues)
    }
    return []
  }

  // 获取教师列表
  async getTeacherList(query) {
    const { ctx } = this
    const { teacherName, schoolName } = query

    let where = {}
    // 添加模糊字段
    const fuzzyField = { teacherName, schoolName }
    where = ctx.helper.addFuzzyField(fuzzyField, where)
    const attributes = { exclude: ['createTime', 'createUser', 'lastTime', 'lastUser', 'deleteFlag'] }
    const options = { where, attributes }
    const res = await ctx.pageFind('Teacher', query, options)
    return res
  }

  // 获取教师主讲课程
  async majorClass(teacherId) {
    const { ctx } = this
    const res = await ctx.find('Course', {
      where: {
        teacherId
      },
      attributes: ['name', 'courseId']
    }, 'findAll')
    if (res) {
      return res.map(item => item.dataValues)
    }
    return []
  }

  // 删除老师重复数据
  async removeTeacher() {
    const { ctx } = this
    const ids = await ctx.model.query('select id from mooc_teacher where teacher_id in (SELECT teacher_id from mooc_teacher GROUP BY teacher_id HAVING COUNT(teacher_id) > 1) and id not in (SELECT min(id) from mooc_teacher GROUP BY teacher_id HAVING COUNT(teacher_id) > 1);')

    if (ids && ids.length > 0) {
      const delIds = ids[0].map(item => `'${item.id}'`)
      const delNum = await ctx.model.query(`delete from mooc_teacher where id in (${delIds.join(',')});`)
      return delNum
    }
    return 0
  }
}

module.exports = MoocCourseService
